# -*- coding: utf-8 -*-
from xTkinter import *
import tkFileDialog
import tkMessageBox as msgBox
import sys
from RequeteSQL import *
from RequeteSQL import *
RequeteSQL = RequeteSQL()
import xmlrpclib
from Validation_Contraintes import *
Validateur =  ValidationContrainte()

class Vue_VoirRapport(Frame):
    def __init__(self,Requete, master,parent,**cnf):
        self.ListeChamps=[]
        self.ListEntries=[]
        self.ListLabels=[]
        self.Operande = None
        self.Contrainte = None
        self.Valeur = None
        self.Requete = Requete
        self.parent = parent
        self.master=master
        self.Where = ""
        Frame.__init__(self,master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=800,height=600)        
        self.BoutonSuivant=xButton(self,text="Suivant",width=10, command=self.Suivant)
        self.BoutonPrecedant=xButton(self,text="Precedant",width=12, command=self.Precedant)
        self.BoutonContrainte=xButton(self,text="Avec Contrainte",width=14, command=self.AjouterContrainte)
        self.BoutonSansContrainte=xButton(self,text="Sans Contrainte",width=12, command=self.SansContrainte)
        self.BoutonSuivant.setLocation(305,0)
        self.BoutonPrecedant.setLocation(0,0)
        self.BoutonContrainte.setLocation(100,0)
        self.BoutonSansContrainte.setLocation(205,0)
        self.Valeur = xEntry(self,width=25)
        self.Valeur.setLocation(635,3)   
        self.ListeEnregistrement = []
        self.Index = None
        self.AfficherRapport()
        
       # self.Regle()
        
        
    def AfficherRapport(self):
        self.ListeChamps=lesChamps = self.Requete.split("select ")[1].split(" from")[0].split(", ")
        
        self.Contrainte=xCombobox(self)
        self.Contrainte.setLocation(400,0)
        self.Contrainte.setWidth(15)
        self.Contrainte.setListWidth(25)
        self.Contrainte.setListHeight(5) 
        self.Contrainte.setText('<Choisir>')
        
        self.Operande = xCombobox(self)
        self.Operande.setLocation(520,0)
        self.Operande.setWidth(15)
        self.Operande.setListWidth(25)
        self.Operande.setListHeight(5) 
        self.Operande.setText('<Choisir>')
        for UneOperande in Validateur.getOperande():
            self.Operande.addItem(UneOperande)
        
        for i in range(len(self.ListEntries)):
            self.ListEntries[i].destroy()
            self.ListLabels[i].destroy()
         
        
        
        self.ListEntries=[]
        self.ListLabels=[]
        for i in range(len(self.ListeChamps)):
            self.ListLabels.append(xLabel(self,text=self.ListeChamps[i], width=len(self.ListeChamps[i])+5, anchor=SW ))
            self.ListLabels[len(self.ListLabels)-1].setLocation(0,(i+1)*25+5)
            self.Contrainte.addItem(self.ListeChamps[i])
        for i in range(len(self.ListeChamps)):
            self.ListEntries.append(xEntry(self,width=50))
            self.ListEntries[len(self.ListEntries)-1].setLocation(120,(i+1)*25+5)
        self.ObtenirEnregistrement()
        self.Index = 0
        self.Afficher()
           
    def close(self):
        self.destroy()
        pass        
    
    def Suivant(self):
        if (self.Index < len(self.ListeEnregistrement) - 1):
            self.Index = self.Index + 1
            self.Afficher()
    
    def Precedant(self):
        if (self.Index > 0):
            self.Index = self.Index - 1
            self.Afficher()
    
    def ObtenirEnregistrement(self):
        Requete = self.Requete + self.Where
        print "requete", Requete
        Reponse = self.parent.serveur.ExecuterRequeteSQLWhere(Requete)
        print "reponse", Reponse
        if(Reponse[0]==1):
          self.ListeEnregistrement = Reponse[1]
        else:
          msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
        if(Reponse[1] == []):
            msgBox.showerror('PERP 2009 | Édition des tables', 'Aucun enregistrement ne repond a ces criteres\n')
    def AjouterContrainte(self):
        if self.Valeur.getText() == "":
            self.Where = ""
        else:
            self.Where = " and " + self.Contrainte.getText() + " " + self.Operande.getText() + " " + self.Valeur.getText()
        self.ObtenirEnregistrement()
        self.AfficherRapport()
        
    def Afficher(self):
        for i in range(len(self.ListeChamps)):
            self.ListEntries[i].setText(self.ListeEnregistrement[self.Index][i])
 
    def SansContrainte(self):
        self.Where = ""
        self.ObtenirEnregistrement()
        self.AfficherRapport()
    
    def Regle(self):
        for x in range (800/25):
            X = x*25
            xLabel(self, text=str(X)).setLocation(X,0)
        for y in range (600/25):
            Y = y*25
            xLabel(self, text=str(Y)).setLocation(0,Y)       
    
#class Controleur:
#    def __init__(self):
#        self.user = "ADMIN"
#        self.profil = "ADMIN"
#        Requete = "select S_EMP.COMMISSION_PCT, S_EMP.SALARY, S_EMP.DEPT_ID, S_EMP.TITLE, S_EMP.MANAGER_ID, S_EMP.COMMENTS, S_EMP.START_DATE, S_EMP.USERID, S_EMP.FIRST_NAME, S_EMP.LAST_NAME, S_EMP.ID, S_DEPT.REGION_ID, S_DEPT.NAME, S_DEPT.ID from S_EMP, S_DEPT where S_EMP.DEPT_ID = S_DEPT.ID"
#        Serveur='http://localhost:8000'
#        self.serveur=xmlrpclib.ServerProxy(Serveur)
#        root=Tk()
#        root.geometry('1200x600')
#        vi=Vue_VoirRapport(Requete,root,self)
#        root.mainloop()       
#
#Controleur = Controleur()


    
    
    
